package com.digital.smartgrow.controller.expert;

import com.digital.smartgrow.common.advice.Result;
import com.digital.smartgrow.dto.expert.ExpertAttendDTO;
import com.digital.smartgrow.dto.expert.ExpertPlanPageDTO;
import com.digital.smartgrow.entity.expert.ExpertPlan;
import com.digital.smartgrow.service.expert.ExpertPlanService;
import com.digital.smartgrow.vo.expert.ExpertGroupVo;
import com.digital.smartgrow.vo.expert.ExpertPlanVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 专家方案 前端控制器
 * </p>
 *
 * @author sunyuechao
 * @since 2022-07-18
 */
@Api(tags = "专家方案")
@RestController
@RequestMapping("${context.prefix}/business/expertPlan")
public class ExpertPlanController {

    @Autowired
    private ExpertPlanService expertPlanService;

    @ApiOperation(value = "专家方案-分页查询")
    @PostMapping("/list")
    public Object getListByPage(@RequestBody ExpertPlanPageDTO expertPlanPageDTO){
        return expertPlanService.getListByPage(expertPlanPageDTO);
    }

    @ApiOperation(value = "专家方案-详情")
    @GetMapping("/{id}")
    public Object getInfo(@PathVariable Integer id){
        if (id == null) return Result.fail("id不能为空");
        return expertPlanService.getInfo(id);
    }

    @ApiOperation(value = "专家方案-删除")
    @DeleteMapping("/{id}")
    public Object removeById(@PathVariable Integer id){
      if (id == null) return Result.fail("id不能为空");
        ExpertPlan byId = expertPlanService.getById(id);
        if(byId.getStatus()!=0){
            return Result.fail("非待抽选状态，不允许删除");
        }
        return expertPlanService.removeAll(id);
    }
    @ApiOperation(value = "专家方案-新增")
    @PostMapping()
    public Object add( @RequestBody ExpertPlanVo expertPlanVo){
       return expertPlanService.saveAll(expertPlanVo);
    }

    @ApiOperation(value = "专家方案-编辑")
    @PutMapping()
    public Object edit(@RequestBody ExpertPlanVo expertPlanVo){
    if (expertPlanVo==null||expertPlanVo.getId() == null)
       return Result.fail("id不能为空");
        ExpertPlan byId = expertPlanService.getById(expertPlanVo.getId());
        if(byId.getStatus()!=0){
            return Result.fail("非待抽选状态，不允许编辑");
        }
       return expertPlanService.updateAll(expertPlanVo);
    }


    @ApiOperation(value = "专家抽选表-开始抽选")
    @GetMapping("/chooseStart/{id}")
    public Object chooseStart( @PathVariable Integer id){
        if (id == null) return Result.fail("id不能为空");
        List<ExpertGroupVo> res=expertPlanService.chooseStart(id);
        return res;
    }

    @ApiOperation(value = "专家抽选表-停止抽选")
    @PostMapping("chooseEnd")
    public Object chooseEnd( @RequestBody List<ExpertGroupVo> expertChoose){
        List<ExpertGroupVo> res= expertPlanService.chooseEnd(expertChoose);
        return res;
    }

    @ApiOperation(value = "专家抽选表-保存抽选")
    @PostMapping("chooseSave")
    public Object chooseSave( @RequestBody  List<ExpertGroupVo> expertChoose){
        return expertPlanService.chooseSave(expertChoose);
    }

    @ApiOperation(value = "专家出席核定")
    @PostMapping("/attend")
    public Object attend( @RequestBody ExpertAttendDTO expertAttendDTO){
        return expertPlanService.attend(expertAttendDTO);
    }
}
